package com.tzxs.pay.main.installment;

import android.app.Application;
import android.arch.lifecycle.AndroidViewModel;
import android.databinding.ObservableField;
import android.support.annotation.NonNull;
import android.util.Log;

import com.google.gson.Gson;
import com.tzxs.pay.main.bill.model.GtBean;
import com.tzxs.pay.main.bill.source.BillDataSource;
import com.tzxs.pay.main.installment.model.InstallBean;
import com.tzxs.pay.util.SingleLiveEvent;
import com.tzxs.pay.util.netnavigator.SimpleNetNavigator;
import com.tzxs.pay.util.viewmodel.Callback;


public class ScanViewModel extends AndroidViewModel {

    private static final String TAG = ScanViewModel.class.getSimpleName();
    public SingleLiveEvent<String> dialogText = new SingleLiveEvent<>();
    public SingleLiveEvent<String> snackText = new SingleLiveEvent<>();

    //花呗分期bean
    public ObservableField<InstallBean.DataBean> bean = new ObservableField<>();

    public ScanViewModel(@NonNull Application application) {
        super(application);
    }


    //扫码分期
    public void startScanInstall(String amount, int installNum, String code, int money, Callback callback) {
        InstallDataResource.getINSTANCE(getApplication()).startScanInstall(amount, installNum, code, money,
                new SimpleNetNavigator<InstallBean>(snackText, dialogText, callback) {
                    @Override
                    public String getTitle() {
                        return "请稍等....";
                    }

                    @Override
                    public void start(String title) {

                        dialogText.setValue(title);
                    }

                    @Override
                    public void fail(String error) {
                        //分期失败
                        snackText.setValue(error);
                    }

                    @Override
                    public void success(InstallBean data) {

                        if (data.getCode() == 10001) {
                            bean.set(data.getData().get(0));
                            callback.success();
                            snackText.setValue("分期成功!");
                        } else {
                            snackText.setValue(data.getMsg());
                        }

                        Log.d(TAG, "success: 扫码分期获取到的数据：" + new Gson().toJson(data));


                    }
                });

    }
    //开始推送消息
    public void startGt(String cid, String title, String content, Callback callback) {
        BillDataSource.getINSTANCE(getApplication()).startGt(cid, title, content,
                new SimpleNetNavigator<GtBean>(dialogText, snackText, callback) {
                    @Override
                    public String getTitle() {
                        return "";
                    }

                    @Override
                    public void success(GtBean data) {
                        if (data.getCode().equals("10001")) {
                            callback.success();
                        } else {
                            snackText.setValue(data.getMsg());
                        }
                    }

                    @Override
                    public void fail(String error) {
                        snackText.setValue(error);
                    }
                });

    }
}
